﻿using MvvmCross.Platform;
using MvvmCross.Plugins.Messenger;
using MvvmCross.Core.ViewModels;
using Sidvall.WorkItemManager.UI.Messaging;
using System.Windows.Input;

namespace Sidvall.WorkItemManager.UI.ViewModels
{
    public class NavigationViewModel : Sidvall.WorkItemManager.UI.ViewModels.Core.ViewModelBase
    {
        #region Public Members

        #region CanShowStart

        private bool _CanShowStart;
        public bool CanShowStart
        {
            get
            {
                return _CanShowStart;
            }
            set
            {
                _CanShowStart = value;
                RaisePropertyChanged("CanShowStart");
            }
        }

        #endregion
        #region CanShowWorkers

        private bool _CanShowWorkers;
        public bool CanShowWorkers
        {
            get
            {
                return _CanShowWorkers;
            }
            set
            {
                _CanShowWorkers = value;
                RaisePropertyChanged("CanShowWorkers");
            }
        }

        #endregion
        #region CanShowWorkItemTypes

        private bool _CanShowWorkItemTypes;
        public bool CanShowWorkItemTypes
        {
            get
            {
                return _CanShowWorkItemTypes;
            }
            set
            {
                _CanShowWorkItemTypes = value;
                RaisePropertyChanged("CanShowWorkItemTypes");
            }
        }

        #endregion
        #region CanShowWorkConditions

        private bool _CanShowWorkConditions;
        public bool CanShowWorkConditions
        {
            get
            {
                return _CanShowWorkConditions;
            }
            set
            {
                _CanShowWorkConditions = value;
                RaisePropertyChanged("CanShowWorkConditions");
            }
        }

        #endregion
        #region CanShowWorkRetryPolicies

        private bool _CanShowWorkRetryPolicies;
        public bool CanShowWorkRetryPolicies
        {
            get
            {
                return _CanShowWorkRetryPolicies;
            }
            set
            {
                _CanShowWorkRetryPolicies = value;
                RaisePropertyChanged("CanShowWorkRetryPolicies");
            }
        }

        #endregion

        #region ShowStartCommand

        public ICommand ShowStartCommand
        {
            get
            {
                return new MvxCommand(() => ShowViewModel<DashboardViewModel>());
            }
        }

        #endregion
        #region ShowWorkersCommand

        public ICommand ShowWorkersCommand
        {
            get
            {
                return new MvxCommand(() => ShowViewModel<WorkerViewModel>());
            }
        }

        #endregion
        #region ShowWorkItemTypesCommand

        public ICommand ShowWorkItemTypesCommand
        {
            get
            {
                return new MvxCommand(() => ShowViewModel<WorkItemTypeViewModel>());
            }
        }

        #endregion
        #region ShowWorkConditionsCommand

        public ICommand ShowWorkConditionsCommand
        {
            get
            {
                return new MvxCommand(() => ShowViewModel<WorkConditionViewModel>());
            }
        }

        #endregion
        #region ShowWorkRetryPoliciesCommand

        public ICommand ShowWorkRetryPoliciesCommand
        {
            get
            {
                return new MvxCommand(() => ShowViewModel<WorkRetryPolicyViewModel>());
            }
        }

        #endregion

        #region Start

        public override void Start()
        {
            var messenger = Mvx.Resolve<IMvxMessenger>();
            this.ViewModelResources.Add(messenger.Subscribe<ServerManagerMessage>(OnServerManagerMessage));
            InitializeCommands();
        }

        #endregion

        #endregion
        #region Private  Members

        #region InitializeCommands

        private void InitializeCommands()
        {
            if (Sidvall.WorkItemManager.UI.SystemContext.Current.ServerManagers == null)
            {
                this.CanShowStart = false;
                this.CanShowWorkers = false;
                this.CanShowWorkItemTypes = false;
                this.CanShowWorkConditions = false;
                this.CanShowWorkRetryPolicies = false;
            }
            else if (Sidvall.WorkItemManager.UI.SystemContext.Current.ServerManagers.IsValid)
            {
                this.CanShowStart = true;
                this.CanShowWorkers = true;
                this.CanShowWorkItemTypes = true;
                this.CanShowWorkConditions = true;
                this.CanShowWorkRetryPolicies = true;
            }
            else
            {
                this.CanShowStart = false;
                this.CanShowWorkers = false;
                this.CanShowWorkItemTypes = false;
                this.CanShowWorkConditions = false;
                this.CanShowWorkRetryPolicies = false;
            }
        }

        #endregion

        #region OnServerManagerMessage

        private void OnServerManagerMessage(ServerManagerMessage item)
        {
            InitializeCommands();
        }

        #endregion

        #endregion
    }
}
